<?php

/**
 * This is the model class for table "{{attribute}}".
 *
 * The followings are the available columns in table '{{attribute}}':
 * @property integer $za_id
 * @property integer $zca_id
 * @property integer $za_type
 * @property string $za_text_type
 * @property string $za_title
 * @property string $za_entitle
 * @property string $za_value
 * @property integer $za_search
 * @property integer $za_listorder
 * @property integer $za_photo
 */
class Attribute extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Attribute the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{attribute}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zca_id, za_type, za_search, za_listorder, za_photo', 'numerical', 'integerOnly'=>true),
			array('za_text_type', 'length', 'max'=>8),
			array('za_title', 'length', 'max'=>50),
			array('za_entitle', 'length', 'max'=>100),
			array('za_value', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('za_id, zca_id, za_type, za_text_type, za_title, za_entitle, za_value, za_search, za_listorder, za_photo', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'za_id' => 'ID',
			'zca_id' => '分类ID',
			'za_type' => '类型',//1属性2规格
			'za_text_type' => '控件类型',
			'za_title' => '名称',
			'za_entitle' => '英文名',
			'za_value' => '值',//用逗号分隔
			'za_search' => '是否支持搜索',//1否2是
			'za_listorder' => '排序',
			'za_photo' => '是否支持图片',//1否2是
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($pagesize=100)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('za_id',$this->za_id);
		$criteria->compare('zca_id',$this->zca_id);
		$criteria->compare('za_type',$this->za_type);
		$criteria->compare('za_text_type',$this->za_text_type,true);
		$criteria->compare('za_title',$this->za_title,true);
		$criteria->compare('za_entitle',$this->za_entitle,true);
		$criteria->compare('za_value',$this->za_value,true);
		$criteria->compare('za_search',$this->za_search);
		$criteria->compare('za_listorder',$this->za_listorder);
		$criteria->compare('za_photo',$this->za_photo);

		$criteria->order = " za_listorder ASC ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>$pagesize)
		));
	}
	/*
	 * 获取类型
	 * */
	public function getType($type=null){
		if($type==null){
			return array(
				'1'=>'属性',
				'2'=>'规格'	
			);
		}else{
			$level = $this->getType();
			return $level[$type];
		}
	}
	/*
	 * 获取是否支持搜索
	* */
	public function getIsSreach($type=null){
		if($type==null){
			return array(
					'1'=>'否',
					'2'=>'是'
			);
		}else{
			$level = $this->getIsSreach();
			return $level[$type];
		}
	}
	/*
	 * 获取类型
	* */
	public function getIsPhoto($type=null){
		if($type==null){
			return array(
					'1'=>'否',
					'2'=>'是'
			);
		}else{
			$level = $this->getType();
			return $level[$type];
		}
	}
	/*
	 * 获取规格选择类型类型
	* */
	public function getTextType($type=null){
		if($type==null){
			return array(
					'checkbox'=>'复选框',
					'select'=>'下拉框',
					'radio'=>'单选框',
					'textarea'=>'多行文本',
					'input'=>'单行文本',
			);
		}else{
			$level = $this->getTextTpye();
			return $level[$type];
		}
	}
	/*
	 * 载入行业规格
	 * @param $categoryid 行业ID
	 * */
	public function loadStaByCateId($categoryid){
		$criteria=new CDbCriteria;
		$criteria->compare('zca_id',$categoryid);
		$criteria->compare('za_type','2');
		$criteria->order = 'za_listorder ASC';
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'pagination'=>array('pageSize'=>100)
		));
	}
	/*
	 * 载入行业属性
	 * @param $categoryid 行业ID
	 * */
	public function loadAttByCateId($categoryid){
		$criteria=new CDbCriteria;
		$criteria->compare('zca_id',$categoryid);
		$criteria->compare('za_type','1');
		$criteria->order = 'za_listorder ASC';
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'pagination'=>array('pageSize'=>100)
		));
	}
	/*
	 * 获取规格属性
	 * @param $categoryid 行业ID
	 * @param $type 分类:1属性,2规格,3组合
	 * @param $search 是否搜索:1否2是
	 * */
	public function getSta($categoryid, $type ='2',$search = '1'){
		$criteria=new CDbCriteria;
		$criteria->compare('zca_id',$categoryid);
		$criteria->compare('za_type',$type);
		$criteria->compare('za_search',$search);
		$criteria->order = 'za_listorder ASC';
		$attr = $this->findAll($criteria);		
		if(!empty($attr)){
			return simplifyData($attr);
		}
	}
	/*
	 * 保存数据前执行以下操作
	 * */
	public function beforeSave(){
		if(parent::beforeSave()){
			$this->za_value = str_replace('，',',',$this->za_value);
			return true;
		}else{
			return false;
		}
	}
}